package com.haohao.web.controller.system;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.haohao.common.annotation.Anonymous;
import com.haohao.system.domain.SysAppliances;
import com.haohao.system.service.ISysAppliancesService;
import com.haohao.system.service.ISysRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haohao.common.annotation.Log;
import com.haohao.common.core.controller.BaseController;
import com.haohao.common.core.domain.AjaxResult;
import com.haohao.common.enums.BusinessType;
import com.haohao.system.domain.SysOrders;
import com.haohao.system.service.ISysOrdersService;
import com.haohao.common.utils.poi.ExcelUtil;
import com.haohao.common.core.page.TableDataInfo;

/**
 * 销售订单Controller
 *
 * @author haohao
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/system/orders")
public class SysOrdersController extends BaseController
{
    @Autowired
    private ISysOrdersService sysOrdersService;
    @Autowired
    private ISysAppliancesService sysAppliancesService;
    @Autowired
    private ISysRoleService sysRoleService;
    /**
     * 查询销售订单列表
     */
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(SysOrders sysOrders)
    {
        if (sysRoleService.countUserIdByRoleId(getUserId())==2){

        }
        else {
            sysOrders.setUid(getUserId());
        }
        startPage();
        List<SysOrders> list = sysOrdersService.selectSysOrdersList(sysOrders);
        return getDataTable(list);
    }

    /**
     * 获取销售订单详细信息
     */
    @GetMapping(value = "/{id}")
    @Anonymous

    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysOrdersService.selectSysOrdersById(id));
    }

    /**
     * 新增销售订单
     */
    @Log(title = "销售订单", businessType = BusinessType.INSERT)
    @PostMapping
    @Anonymous

    public AjaxResult add(@RequestBody SysOrders sysOrders)
    {
        SysAppliances sysAppliances = sysAppliancesService.selectSysAppliancesById(sysOrders.getTid());
        sysOrders.setName(sysAppliances.getName());
        sysOrders.setPic(sysAppliances.getPic());
        sysOrders.setPic(sysAppliances.getPic());
        sysOrders.setContent(sysAppliances.getContent());
        sysOrders.setPrice(sysAppliances.getPrice());
        BigDecimal a = new BigDecimal(String.valueOf(sysAppliances.getPrice()));
        BigDecimal b = new BigDecimal(String.valueOf(sysAppliances.getCount()));
        sysOrders.setZprice(a.multiply(b));
        sysOrders.setBrand(sysAppliances.getBrand());
        sysOrders.setPlace(sysAppliances.getPlace());
        sysOrders.setSize(sysAppliances.getSize());
        sysOrders.setWeight(sysAppliances.getWeight());
        sysOrders.setTimetomarket(sysAppliances.getTimetomarket());
        sysOrders.setUid(getUserId());
        sysOrders.setState("创建成功");
        sysOrders.setCreateBy(getUsername());
        sysAppliances.setCount(sysAppliances.getCount() - sysOrders.getCount());
        sysAppliancesService.updateSysAppliances(sysAppliances);

        return toAjax(sysOrdersService.insertSysOrders(sysOrders));
    }

    /**
     * 修改销售订单
     */
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @Anonymous

    public AjaxResult edit(@RequestBody SysOrders sysOrders)
    {
        return toAjax(sysOrdersService.updateSysOrders(sysOrders));
    }

    /**
     * 删除销售订单
     */
    @Anonymous
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long ids)
    {
        return toAjax(sysOrdersService.deleteSysOrdersById(ids));

    }
}
